<template>
  <div class="register-container">
    <div class="register-card">
      <div class="register-header">
        <h1>创建账户</h1>
        <p>注册2FA管理系统</p>
      </div>
      
      <form @submit.prevent="handleRegister" class="register-form">
        <div class="form-group">
          <label class="form-label">用户名</label>
          <input
            v-model="registerForm.username"
            type="text"
            class="form-input"
            placeholder="至少3个字符"
            required
          />
        </div>
        
        <div class="form-group">
          <label class="form-label">邮箱</label>
          <input
            v-model="registerForm.email"
            type="email"
            class="form-input"
            placeholder="your@email.com"
            required
          />
        </div>
        
        <div class="form-group">
          <label class="form-label">密码</label>
          <input
            v-model="registerForm.password"
            type="password"
            class="form-input"
            placeholder="至少8个字符，包含大小写字母和数字"
            required
          />
          <div v-if="passwordErrors.length > 0" class="password-hints">
            <div v-for="err in passwordErrors" :key="err" class="hint-item">
              ❌ {{ err }}
            </div>
          </div>
        </div>
        
        <div class="form-group">
          <label class="form-label">确认密码</label>
          <input
            v-model="registerForm.confirmPassword"
            type="password"
            class="form-input"
            placeholder="再次输入密码"
            required
          />
        </div>
        
        <div class="form-error" v-if="error">
          {{ error }}
        </div>
        
        <button
          type="submit"
          class="btn btn-primary btn-block"
          :disabled="loading || !isFormValid"
        >
          {{ loading ? '注册中...' : '注册' }}
        </button>
      </form>
      
      <div class="register-footer">
        <router-link to="/login">已有账户？立即登录</router-link>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, computed, watch } from 'vue';
import { useRouter } from 'vue-router';
import { useAuthStore } from '@/store/auth';
import { validatePassword } from '@/utils/helpers';

const router = useRouter();
const authStore = useAuthStore();

const registerForm = ref({
  username: '',
  email: '',
  password: '',
  confirmPassword: '',
});

const loading = ref(false);
const error = ref('');

const passwordErrors = computed(() => {
  if (!registerForm.value.password) return [];
  const validation = validatePassword(registerForm.value.password);
  return validation.errors;
});

const isFormValid = computed(() => {
  return (
    registerForm.value.username.length >= 3 &&
    registerForm.value.email.includes('@') &&
    passwordErrors.value.length === 0 &&
    registerForm.value.password === registerForm.value.confirmPassword
  );
});

watch(() => registerForm.value.password, () => {
  error.value = '';
});

async function handleRegister() {
  if (!isFormValid.value) return;
  
  loading.value = true;
  error.value = '';
  
  try {
    await authStore.register({
      username: registerForm.value.username,
      email: registerForm.value.email,
      password: registerForm.value.password,
    });
    
    // 注册成功，跳转到登录页
    router.push({
      path: '/login',
      query: { message: '注册成功，请登录' },
    });
  } catch (err) {
    error.value = err.response?.data?.error?.message || '注册失败，请重试';
  } finally {
    loading.value = false;
  }
}
</script>

<style scoped>
.register-container {
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  padding: 20px;
}

.register-card {
  background: white;
  border-radius: 12px;
  padding: 40px;
  width: 100%;
  max-width: 450px;
  box-shadow: 0 10px 40px rgba(0, 0, 0, 0.2);
}

.register-header {
  text-align: center;
  margin-bottom: 32px;
}

.register-header h1 {
  font-size: 28px;
  color: #333;
  margin-bottom: 8px;
}

.register-header p {
  color: #666;
  font-size: 14px;
}

.register-form .form-group {
  margin-bottom: 20px;
}

.password-hints {
  margin-top: 8px;
  font-size: 12px;
}

.hint-item {
  color: #f5222d;
  padding: 2px 0;
}

.btn-block {
  width: 100%;
  padding: 12px;
  font-size: 16px;
  margin-top: 24px;
}

.form-error {
  color: #f5222d;
  font-size: 14px;
  margin-top: 8px;
  padding: 8px 12px;
  background: #fff1f0;
  border-radius: 4px;
  border-left: 3px solid #f5222d;
}

.register-footer {
  margin-top: 24px;
  text-align: center;
}

.register-footer a {
  color: #1890ff;
  text-decoration: none;
  font-size: 14px;
}

.register-footer a:hover {
  text-decoration: underline;
}
</style>


